package com.example.teamwork.impl;

import com.example.teamwork.mapper.InterMapper;
import com.example.teamwork.pojo.Inter;
import com.example.teamwork.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 〈表格管理〉<br>
 * @author  SongPeng
 * @date  2021/11/21 15:37
 * @version 1.0
 */

@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
       private InterMapper interMapper;
    private static Calendar fromCal = Calendar.getInstance();
    @Override
    public void ExcelInsert(List row) {
        //打印信息，获取数据第二行以后实际数据
        for (int i = 1;i<row.size();i++){
            List<String> cell = (List<String>) row.get(i);
            System.out.println(cell);
            Inter inter=new Inter();
            inter.setInterName(cell.get(0));
            if(cell.get(1)=="男"){
                inter.setInterSex(0);
            }else {
                inter.setInterSex(1);
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            try {
                //处理出生日期5位数问题
                if(cell.get(2).length() == 5){
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = dateFormat.parse("1900-01-01");
                    int day = Integer.parseInt(cell.get(2));
                    Date birthDate = addDate(date, (day-2));
                    System.out.println(birthDate);
                    inter.setInterBirth(birthDate);
                }else{
                    Date birth = simpleDateFormat.parse(getStringDate(cell.get(2)));
                    inter.setInterBirth(birth);
                }


            } catch (ParseException e) {
                e.printStackTrace();
            }
            inter.setInterAge(Integer.valueOf(cell.get(3)));
            inter.setInterPhone(cell.get(4));
            inter.setPartId(Long.valueOf(cell.get(5)));
            interMapper.insertSelective(inter);
        }

    }
    public static String getStringDate(String sdate){
        String sreturn="";
        if(sdate.length()==9){
            String s_nd = sdate.substring(0, 4); // 年份
            String s_yf = sdate.substring(5, 6); // 月份
            String s_rq = sdate.substring(7, 9); // 日期
            sreturn=s_nd+"-"+s_yf+"-"+s_rq;
            return sreturn;
        }else{
            String s_nd = sdate.substring(0, 4); // 年份
            String s_yf = sdate.substring(5, 7); // 月份
            String s_rq = sdate.substring(8, 10); // 日期
            sreturn=s_nd+"-"+s_yf+"-"+s_rq;
            return sreturn;
        }

    }
    //处理出生日期5位数问题
    public static Date addDate(Date date, int day) {
        try {
            fromCal.setTime(date);
            fromCal.add(Calendar.DATE, day);

            return fromCal.getTime();
        } catch (Exception e) {
            return null;
        }
    }

}
